#!/usr/bin/python
import MuConnection, MuNetworkConfiguration, MuRestApi, zipfile, time, sys 


class pva():

	USERNAME = 'admin'
	PASSWORD = ''
	MU = ''
	myMuConnection = None
	results = []
	summary = []
	error = True
	faultCount = 0
	blockFail = 0

	def __init__(self,mu,username,password,analysisUUID,source,destination,pvaName,blockViolation=0,faultCount=0,verbose=False):
		""" set up the Test """
		self.USERNAME = username
		self.PASSWORD = password
		self.MU = mu
		#print "-------"," ",mu," ",username," ",password
		
		myMuConnection = MuConnection.MuConnection(self.MU,self.USERNAME,self.PASSWORD)
		self.myMuConnection = myMuConnection
		myRestApi = MuRestApi.MuRestApi(myMuConnection)
		myRestApi.VERBOSE = verbose
		myMuConnection.VERBOSE = verbose
		
		
		#------- Run the PVA ----------
		response = myRestApi.runAnalysis(analysisUUID)
		if response.isCompleted() and not response.hasError():
			newUUID = response.getUUID()
			loop = True
			while loop:
				time.sleep(10)
				# check status
				response = myRestApi.getAnalysisStatus(newUUID)
				status = response.getStatus()
				if status != 'RUNNING':
					loop = False
				print "------  " + status + " -------- "
		
			# ------- get the faults
			blockFail = 0
			response = myRestApi.getAnalysisFaultList(newUUID)
			faultList = response.getRetrievedObject()
			self.results = faultList
			self.faultCount = len(faultList)
			for fault in faultList:
				faultType = fault.getFaultDetection()
				if faultType=="Blocking Violation":
					self.blockFail = self.blockFail + 1
			final = "PASSED"
			if self.faultCount > faultCount:
				final = "FAILED"
			if self.blockFail > blockViolation:
				final = "FAILED"
			self.summary = ["OK",final,"FaultCount="+str(self.faultCount),"BlockingViolations="+str(self.blockFail)]		
		else:
			print "ERROR running test"
			self.summary = ["ERROR",response.getRawResponse()]
		print '------ ' + self.summary[0] + ' / ' + self.summary[1] + ' --------'